//
//  SettingViewController.m
//  A段项目
//
//  Created by dllo on 16/3/9.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "SettingViewController.h"

#import <UIImageView+WebCache.h>
#import <DKNightVersion.h>

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;

@end

@implementation SettingViewController

- (void)dealloc {
    
    [_tableView release];
    
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.navigationItem.title = @"设置";
    [self creatTableView];
    
    
    
}

- (void)creatTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:0];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource  = self;
    
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool1"];
    
    [_tableView release];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    cell.dk_backgroundColorPicker =DKColorWithColors([UIColor whiteColor],[UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
    cell.textLabel.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    
    if (indexPath.row == 1) {
        
        UISwitch *switch1 = [[UISwitch alloc] initWithFrame:CGRectMake(WIDTH - 10 - 50, 5, 50, 50)];
        
        
        [cell addSubview:switch1];
        
        [switch1 addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        
        [NSUserDefaults standardUserDefaults];
        
        /** 判断开关状态 */
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
            
            switch1.on = YES;
        } else {
            
            switch1.on = NO;
        }

        
        cell.selectionStyle = 0;
        
        cell.textLabel.text = @"夜间模式";
        cell.imageView.image = [UIImage imageNamed:@"night"];
        
        return cell;
        
        
    }else {
      
        cell.textLabel.text = @"清空缓存";
        cell.imageView.image = [UIImage imageNamed:@"clear"];
        
        return cell;
    }
    

}

- (void)switchAction:(UISwitch *)switch1 {
        
        if ([DKNightVersionManager currentThemeVersion] == DKThemeVersionNight) {
            
            [DKNightVersionManager dawnComing];
            
            [[NSUserDefaults standardUserDefaults] setBool:switch1.on forKey:@"夜间模式"];
            
        }else {
            
            [DKNightVersionManager nightFalling];
            
            [[NSUserDefaults standardUserDefaults] setBool:switch1.on forKey:@"夜间模式"];
        }
        
        
        [[NSUserDefaults standardUserDefaults]synchronize];

        
        
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {
    
    if (indexPath.row == 0) {
        
        // 获取缓存大小
        NSUInteger size = [[SDImageCache sharedImageCache] getSize];
        
        float cacheSize = size / 1024.0 / 1024.0;
        
        NSString  *string = [NSString stringWithFormat:@"%.2fM",cacheSize];
        
        NSString *info = [NSString stringWithFormat:@"你有 %@ 的缓存,点击确定清除",string];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:info preferredStyle:1];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:0 handler:^(UIAlertAction * _Nonnull action) {
            
            [[SDImageCache sharedImageCache] clearDisk];
            
            
            
            UIAlertController *alert2 = [UIAlertController alertControllerWithTitle:@"提示" message:@"您已成功清除缓存" preferredStyle:1];
            
            [self presentViewController:alert2 animated:YES completion:^{
                
                [alert2 dismissViewControllerAnimated:YES completion:nil];
                
            }];
            
            
        }];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:1 handler:^(UIAlertAction * _Nonnull action) {
            
           
        }];
        
        [alert addAction:action1];
        [alert addAction:action2];
        
        [self presentViewController:alert animated:YES completion:^{
            
            
            
        }];
        

        
    }
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    
    
    self.view.dk_backgroundColorPicker = DKColorWithColors([UIColor blackColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
    self.tableView.dk_backgroundColorPicker =DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    

    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
